package solutions;

import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import objects.Table;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;

public class CreateSolution {

    public static void CreateDirectory(String path, String dbName) {
        File file = new File(path + "/" + dbName);
        System.out.println(file.getAbsolutePath());
        if(!file.exists()){
            file.mkdir();
            System.out.println("Query OK");
        }
        else{
            System.out.println("ERROR: database exists");
        }
    }

    public static void CreateTable(String path, String dbName, String tableName, List<ColumnDefinition> columns) throws IOException {
        File file = new File(path + "/" + dbName + "/" + tableName);
        if(!file.exists()){
            file.mkdir();
            File filedesc = new File(path+"/"+dbName+"/"+tableName+"/"+tableName+".tsc");
            File filedata = new File(path+"/"+dbName+"/"+tableName+"/" +tableName+".dat");
            if(!filedesc.exists()){
                filedesc.createNewFile();
                Table table = new Table();
                try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(filedesc))) {
                    outputStream.writeObject(table);
                    System.out.println("Table serialized and saved to " + filedesc.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(!filedata.exists()){
                filedata.createNewFile();
            }
            System.out.println("Query OK");
        }

        else{
            System.out.println("ERROR: database exists");
        }
    }
}
